import logging

from fastapi import APIRouter, Query
from fastapi.params import Depends
from pydantic import BaseModel, Field
from sqlalchemy import text
from sqlalchemy.orm import Session
import requests

from commons.BaseResponse import BaseResponse
from config import SCREEN_PREFIX
from datebase import get_db

data_screen_router = APIRouter(prefix="/screen", tags=["大屏数据 "])


class WarnSearch(BaseModel):
    fsiteNo: str = Field(None, description="地点编号")
    fsiteLevel: str = Field(None, description="区域等级")


@data_screen_router.get("/getWarningClassDataByFsiteNo", summary="数据大屏 获取报警分类数量")
def get_warning_class_data_by_fsiteNo(warn: WarnSearch = Query(), db: Session = Depends(get_db)):
    sql = "select fnewwork,f from proc_rpt_equipmentwarring( :fsiteNo )"
    sqlResult = db.execute(text(sql), {
        "fsiteNo": warn.fsiteNo
    }).fetchall()
    resultData = []
    if (sqlResult is not None and len(sqlResult) > 0):
        for key in range(len(sqlResult)):
            fnewwork = sqlResult[key][0]
            f = sqlResult[key][1]
            item = {"fnewwork": fnewwork, "f": f, "eqtInvVolist": None, "fsiteName": None}
            resultData.append(item)
    commonResult = {"list": resultData}
    return BaseResponse.success(data=commonResult)


@data_screen_router.get("/getAverageVoltageByFsiteNo", summary="数据大屏 调用java接口")
def get_average_voltage_by_fsiteNo(warn: WarnSearch = Query(), db: Session = Depends(get_db)):
    try:
        params = {"fsiteNo": warn.fsiteNo}
        response = requests.get(SCREEN_PREFIX + "/getAverageVoltageByFsiteNo", params=params)
        jsondata = response.json()
        if response.status_code == 200:
            code = jsondata["code"]
            data = jsondata["data"]
            msg = jsondata["msg"]
            return BaseResponse.success(data=data)
        else:
            return BaseResponse.error(code=response.status_code, message="调用失败")
    except Exception as e:
        logging.error(">>>>> 网络连接redis失败")
        return BaseResponse.error(code=500, message=str(e))


@data_screen_router.get("/getNetworkRateByFsiteNo", summary="数据大屏 获取区域电量设备联网数 调用java接口")
def get_network_rate_by_fsiteNo(warn: WarnSearch = Query(), db: Session = Depends(get_db)):
    paramers = {"fsiteNo": warn.fsiteNo, "fsiteLevel": warn.fsiteLevel}
    try:
        response = requests.get(SCREEN_PREFIX + "/getNetworkRateByFsiteNo", params=paramers)
        jsondata = response.json()
        if response.status_code == 200:
            code = jsondata["code"]
            data = jsondata["data"]
            msg = jsondata["msg"]
            return BaseResponse.success(data=data)
        else:
            return BaseResponse.error(code=response.status_code, message=jsondata["msg"])
    except Exception as e:
        logging.error(">>>>> 网络连接redis失败")
        return BaseResponse.error(code=500, message=str(e))


@data_screen_router.get("/getRedis", summary="数据大屏 获取区域电量设备联网数")
def get_redis(warn: WarnSearch = Query(), db: Session = Depends(get_db)):
    paramers = {"fsiteNo": warn.fsiteNo, "fsiteLevel": warn.fsiteLevel}
    try:
        response = requests.get(SCREEN_PREFIX + "/getNetworkRateByFsiteNo", params=paramers)
        jsondata = response.json()
        if response.status_code == 200:
            code = jsondata["code"]
            data = jsondata["data"]
            msg = jsondata["msg"]
            return BaseResponse.success(data=data)
        else:
            return BaseResponse.error(code=response.status_code, message=jsondata["msg"])
    except Exception as e:
        logging.error(">>>>> 网络连接redis失败")
        return BaseResponse.error(code=500, message=str(e))
